// pages/mine/mine.js
import { server } from "../../utils/config.js";
// jwt工具
const { hasJwt, removeJwt } = require("../../utils/jwt.js");

Page({

    /**
     * 页面的初始数据
     */
    data: {
        server: server,
        username: "",
        head: "",
        menu: [{
                id: 1,
                type: "contact", // button的open-type值
                title: "联系我们",
                isOpen: true, //使用button开放能力
                icon: "/public/icon/weixin.png"
            },
            {
                id: 2,
                type: "about",
                title: "关于我们",
                isOpen: false, // 不使用开放能力则走下方点击函数
                icon: "/public/icon/about.png",
                path: "/pages/mine/about/about"
            },
            {
                id: 3,
                type: "feedback", // button的open-type值
                title: "意见反馈",
                isOpen: true,
                icon: "/public/icon/feedback.png"
            },
            {
                id: 4,
                type: "setting",
                title: "设置中心",
                isOpen: false,
                icon: "/public/icon/setting.png",
                path: "/pages/mine/setting/setting"
            },
        ],
    },

    /**
     * 前往登录页
     */
    toLoginPage() {
        if (!hasJwt()) {
            wx.navigateTo({
                url: '../login/login',
            })
        } else {
            wx.navigateTo({
                url: '/pages/mine/info/info',
            })
        }
    },

    /**
     * 前往其他页面
     */
    toPageClick(res) {
        let path = res.currentTarget.dataset.path;
        if (path === "") {
            return;
        }
        wx.navigateTo({
            url: path,
        })
    },

    /**
     * 获取用户信息
     */
    getUserInfo() {
        let that = this;
        wx.fetch({ url: "/user" })
            .then(res => {
                that.$storeCommit("setUserName", res.data.username);
                that.$storeCommit("setHead", res.data.head);
            })
            .catch(err => {
                if (err.code === 401 || err.code === 304) {
                    // 登录失效
                    removeJwt();
                }
            });
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that = this;
        that.$effect(() => {
            that.setData({
                username: that.$getStoreData().username,
                head: that.$getStoreData().head,
            })
        });
        if (hasJwt()) {
            that.getUserInfo();
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        if (hasJwt()) {
            this.getUserInfo();
        } else {
            this.$storeCommit("setUserName", "登录/注册");
            this.$storeCommit("setHead", "/public/images/default_avatar.png");
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})